﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace WireLessPC
{
    class ScreenSenderThread
    {
        #region Variables Defined
        private Socket socket = null;
        Thread sendThread = null;
        WireLessPC.ReceiveSender handleScript = null;
        private mainform form = null;
        #endregion

        #region Constructor with Ip Address and corresponding port
        public ScreenSenderThread(String ip, int port, Object form)
        {
            if (ip != null && ip.IndexOf(".") > 0 && port > 0)
            {
                try
                {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPAddress ipaddress = IPAddress.Parse(ip);
                    IPEndPoint ipendpoint = new IPEndPoint(ipaddress, port);
                    socket.Bind(ipendpoint);
                    socket.Listen(GlobalConstants.MAXLISTENSOCKETS);
                    this.form = form as mainform;
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message.ToString());
                }
            }
        }
        #endregion

        #region Start a new Thread to handle sreen sent to the end socket
        public void Handle()
        {
            if (socket != null)
            {
                try
                {
                    while (socket != null && socket.IsBound)
                    {
                        Socket senderSocket = socket.Accept();
                        byte[] screen = new byte[GlobalConstants.BYTESIZE];
                        int size = senderSocket.Receive(screen);
                        String screenSize = null;
                        if (size > 0)
                        {
                            screenSize = Encoding.UTF8.GetString(screen);
                        }
                        sendThread = new Thread(new ThreadStart((handleScript = new WireLessPC.ReceiveSender(senderSocket,screenSize,this.form)).sendScreen));
                        sendThread.Start();
                    }
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message.ToString());
                }
            }
        }
        #endregion

        #region Function for destorying class and release all the resource
        public void Abort()
        {
            if (this.socket != null)
            {
                if (this.socket.Connected)
                {
                    this.socket.Disconnect(true);
                    this.socket = null;
                }
                else
                {
                    this.socket.Dispose();
                    this.socket = null;
                }
            }
            if (this.sendThread != null)
            {
                this.sendThread.Abort();
                if (this.handleScript != null)
                {
                    this.handleScript.Abort();
                }
                this.handleScript = null;
            }
        }
        #endregion
    }
}
